package me.flyray.bsin.infrastructure.biz.paychannel.joinpay.config;

import me.flyray.bsin.infrastructure.biz.paychannel.joinpay.client.JoinPayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import reactor.core.publisher.Mono;

@Configuration
public class JoinPayConfig {

    @Bean
    public JoinPayClient joinPayClient() {
        WebClient webClient = WebClient.builder()
                .filter(responseLoggingFilter())
                .build();
        HttpServiceProxyFactory factory = HttpServiceProxyFactory
                .builder()
                .exchangeAdapter(WebClientAdapter.create(webClient))
                .build();
        return factory.createClient(JoinPayClient.class);
    }

    // 响应拦截器：统一处理/打印日志
    private ExchangeFilterFunction responseLoggingFilter() {
        return ExchangeFilterFunction.ofResponseProcessor(response -> {
            System.out.println("响应状态码: " + response);
            return Mono.just(response);
        });
    }

}
